package com.hot100.easy;

import java.util.Stack;

/**
 * @ClassName Demo12
 * @Description 最小栈
 * @Author zt
 * @Date 16:00 2022/6/10
 * @Version 1.0
 * ["MinStack","push","push","push","push","getMin","pop","getMin","pop","getMin","pop","getMin"]
    [[],[2],[0],[3],[0],[],[],[],[],[],[],[]]
 **/
public class Demo12 {


    class MinStack {

        Stack stack;
        Stack minStack=new Stack();

        public MinStack() {
            stack=new Stack();
        }

        public void push(int val) {
            stack.push(val);
            if (minStack.empty()){
                minStack.push(val);
            }else{
                Integer peek = (Integer) minStack.peek();
                if(val<=peek){
                    minStack.push(val);
                }
            }


        }

        public void pop() {
            Integer pop = (Integer) stack.pop();
            if(!minStack.empty()){
                Integer peek = (Integer) minStack.peek();
                if(peek.equals(pop)){
                    minStack.pop();
                }
            }

        }

        public int top() {

             return (int) stack.peek();
        }

        public int getMin() {

            return (int) minStack.peek();

        }
    }
}